//
//  Version.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/4/9.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "PersonalVersion.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
@implementation PersonalVersion


-(id)init
{
    self = [super init];
    if(self)
    {
    
    }
    return self;
}



//每个数据库版本升级，这里增加一个函数。
-(void)update100:(FMDatabase* )db
{
    if(db != nil)
    {
//        [db executeUpdate:@"CREATE TABLE ACCOUNT (identifer  INTEGER NOT NULL, accountName varchar(50)  NOT NULL, passWord varchar(50)  NOT NULL, gesturePassword varchar(20) DEFAULT NULL, loginStatus INTEGER DEFAULT 0, PRIMARY KEY (identifer));"];
        
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS MESSAGE (MessageIndex integer PRIMARY KEY AUTOINCREMENT, ServerID varchar(20)  NOT NULL, MessageType varchar(64)  NOT NULL, MessageContent varchar(4000) NOT NULL, MessageUri varchar(256)  NOT NULL,  FileSize varchar(25)  NOT NULL,SoundLength INTEGER DEFAULT 0,  FileCacheName varchar(256)  NOT NULL, FileName varchar(256)  NOT NULL,ReadStatus INTEGER DEFAULT 0, SoundPlayStatus INTEGER DEFAULT 0, MessageSendedStatus INTEGER DEFAULT 0, GroupId varchar(64)  NOT NULL, SenderCode varchar(64)  NOT NULL, MineJid varchar(256)  NOT NULL, FriendJid varchar(256)  NOT NULL, TimeStamp varchar(256)  NOT NULL, MessageId varchar(256)  NOT NULL, MineUserId varchar(256)  NOT NULL, FriendUserId varchar(256)  NOT NULL,isMineSend INTEGER DEFAULT 0,FriendUserName varchar(64)  NOT NULL, MineUserName varchar(64)  NOT NULL,IsShowInmessageList INTEGER DEFAULT 0, Stream varchar(10000) NOT NULL, SendStatus varchar(50) NOT NULL, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
         [db executeUpdate:@"CREATE TABLE IF NOT EXISTS NOTICE (NoticeIndex integer PRIMARY KEY AUTOINCREMENT, ServerID varchar(20)  NOT NULL, NoticeType varchar(64)  NOT NULL, NoticeContent varchar(4000) NOT NULL,ReadStatus INTEGER DEFAULT 0, SoundPlayStatus INTEGER DEFAULT 0,GroupId varchar(64)  NOT NULL, SenderCode varchar(64)  NOT NULL, MineJid varchar(256)  NOT NULL, FriendJid varchar(256)  NOT NULL, TimeStamp varchar(256)  NOT NULL, NoticeId varchar(256)  NOT NULL, MineUserId varchar(256)  NOT NULL, FriendUserId varchar(256)  NOT NULL,isMineSend INTEGER DEFAULT 0,FriendUserName varchar(64)  NOT NULL, MineUserName varchar(64)  NOT NULL,IsShowInmessageList INTEGER DEFAULT 0, Stream varchar(10000) NOT NULL, SendStatus varchar(50) NOT NULL, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS GROUPINFO(identifer integer PRIMARY KEY AUTOINCREMENT, GroupId varchar(32)  NOT NULL, GroupIntroduction varchar(500)  NOT NULL,OwnerCode varchar(64)  NOT NULL,OwnerName varchar(20)  NOT NULL, UserNum INTEGER DEFAULT 0,CreateDate varchar(40)  NOT NULL,GroupName varchar(40)  NOT NULL, OwnerId varchar(40)  NOT NULL, GroupNotice varchar(500)  NOT NULL, messageSettingType INTEGER DEFAULT 0,  PinYin varchar(500)  NOT NULL,firstLetter varchar(500)  NOT NULL, IsDel INTEGER DEFAULT 0, isShow INTEGER DEFAULT 0, groupType INTEGER DEFAULT 0, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS GROUPMEMBERS (identifer integer PRIMARY KEY AUTOINCREMENT, GroupId varchar(64)  NOT NULL, UserCode varchar(64)  NOT NULL, IsAdmin INTEGER DEFAULT 0, LastTime varchar(100)  NOT NULL, Available INTEGER DEFAULT 0, UserId varchar(100)  NOT NULL, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS RECENTLYCONTACT (identifer integer PRIMARY KEY AUTOINCREMENT, UserId varchar(64) DEFAULT 0,yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS USERSETTING (identifer integer PRIMARY KEY AUTOINCREMENT, Voice INTEGER DEFAULT 0, Shake INTEGER DEFAULT 0, Disturb INTEGER DEFAULT 0, StartDate varchar(40) NOT NULL, EndDate varchar(40)  NOT NULL, BothOnline INTEGER DEFAULT 0, IsOnlyWifiRcv INTEGER DEFAULT 0, Autograph varchar(200)  NOT NULL, userCode varchar(200) NOT NULL,isUseGesturePassword INTEGER DEFAULT 0,isForgetGuesturePassword INTEGER DEFAULT 0,yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS FILEMANAGER (identifer integer PRIMARY KEY AUTOINCREMENT,  LocalFilePath varchar(200)  NOT NULL,  FileSize INTEGER DEFAULT 0,  DownloadSize INTEGER DEFAULT 0,  FileName varchar(200)  NOT NULL,  LocalCacheName varchar(200)  NOT NULL,  CreateDate varchar(40)  NOT NULL,  MessageIndex INTEGER DEFAULT 0, GroupId varchar(200)  NOT NULL, serverFileName varchar(200)  NOT NULL,fromUserId varchar(32)  NOT NULL, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS COMPANYINFO (identifer integer PRIMARY KEY AUTOINCREMENT,compCode varchar(200)  NOT NULL,callcenterTelephone varchar(200)  NOT NULL, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS MESSAGEDRAFT (MessageIndex integer PRIMARY KEY AUTOINCREMENT, MessageType varchar(64)  NOT NULL, MessageContent varchar(4000) NOT NULL, MineUserId varchar(256)  NOT NULL, FriendUserId varchar(256)  NOT NULL,MessageSendedStatus INTEGER DEFAULT 0, GroupId varchar(64)  NOT NULL, TimeStamp varchar(256)  NOT NULL,FriendUserName varchar(64)  NOT NULL, MineUserName varchar(64)  NOT NULL, IsShowInmessageList INTEGER DEFAULT 0, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS DOWNLOADFILE (DownLoadIndex integer PRIMARY KEY AUTOINCREMENT, LocalFilePath varchar(200)  NOT NULL,  FileSize INTEGER DEFAULT 0,  DownloadSize INTEGER DEFAULT 0,  FileName varchar(200)  NOT NULL,  LocalCacheName varchar(200)  NOT NULL,  CreateDate varchar(40)  NOT NULL,  MessageIndex INTEGER DEFAULT 0, GroupId varchar(200)  NOT NULL, serverFileName varchar(200)  NOT NULL,fromUserId varchar(32)  NOT NULL, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];

        

        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS EFENGSHARECONTENT (CommentIndex integer PRIMARY KEY AUTOINCREMENT, commentId varchar(200)  NOT NULL, shareId varchar(200)  NOT NULL, sendUserId varchar(200)  NOT NULL,  receiveUserId varchar(200)  NOT NULL, sendUserName varchar(200)  NOT NULL, receiveUserName varchar(200)  NOT NULL, commentType varchar(20)  NOT NULL, content varchar(5000)  NOT NULL, date varchar(200)  NOT NULL, isRead INTEGER DEFAULT 0, isClick INTEGER DEFAULT 0, isLocal INTEGER DEFAULT 0, isDel INTEGER DEFAULT 0, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS EFENGSHARE (ShareIndex integer PRIMARY KEY AUTOINCREMENT, shareId varchar(200)  NOT NULL, releaseUserName varchar(200)  NOT NULL,  releaseUserId varchar(200)  NOT NULL, visibleUserId varchar(5000)  NOT NULL, praiseUserId varchar(200)  NOT NULL, praiseCount varchar(20)  NOT NULL, releaseType varchar(20)  NOT NULL, imageName varchar(5000) NOT NULL, content varchar(5000)  NOT NULL, companyId varchar(200)  NOT NULL, TimeStamp varchar(256)  NOT NULL, isRead INTEGER DEFAULT 0, isClick INTEGER DEFAULT 0, isShowShanchu INTEGER DEFAULT 0, isLocal INTEGER DEFAULT 0, isZan INTEGER DEFAULT 0, isDel INTEGER DEFAULT 0, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS EFENGPERSONALSHARE (ShareIndex integer PRIMARY KEY AUTOINCREMENT, shareId varchar(200)  NOT NULL, releaseUserName varchar(200)  NOT NULL,  releaseUserId varchar(200)  NOT NULL, visibleUserId varchar(5000)  NOT NULL, praiseUserId varchar(200)  NOT NULL, praiseCount varchar(20)  NOT NULL, releaseType varchar(20)  NOT NULL, imageName varchar(5000) NOT NULL, content varchar(5000)  NOT NULL, companyId varchar(200)  NOT NULL, TimeStamp varchar(256)  NOT NULL, isRead INTEGER DEFAULT 0, isClick INTEGER DEFAULT 0, isShowShanchu INTEGER DEFAULT 0, isLocal INTEGER DEFAULT 0, isZan INTEGER DEFAULT 0, isDel INTEGER DEFAULT 0, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];

        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS EFENGSHARECOMMENTDEL (commentDelIndex integer PRIMARY KEY AUTOINCREMENT, commentId varchar(200)  NOT NULL, type varchar(200)  NOT NULL, date varchar(200)  NOT NULL, yuLiuZiduan1 varchar(200),yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200));"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS EFENGSHAREDEL (shareDelIndex integer PRIMARY KEY AUTOINCREMENT, shareId varchar(200)  NOT NULL, type varchar(200)  NOT NULL, date varchar(200)  NOT NULL, yuLiuZiduan1 varchar(200),yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200));"];
        
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS ATTENTIONCONTACT (identifer integer PRIMARY KEY AUTOINCREMENT, UserId varchar(64) DEFAULT 0, isAttentionMe INTEGER DEFAULT 0, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];

        if (![db columnExists:@"videoName" inTableWithName:@"EFENGSHARE"]){
            NSString *alertStr = [NSString stringWithFormat:@"ALTER TABLE %@ ADD %@ varchar(5000) DEFAULT '' NOT NULL",@"EFENGSHARE",@"videoName"];
            [db executeUpdate:alertStr];
        }
        if (![db columnExists:@"videoName" inTableWithName:@"EFENGPERSONALSHARE"]){
            NSString *alertStr = [NSString stringWithFormat:@"ALTER TABLE %@ ADD %@ varchar(5000) DEFAULT '' NOT NULL",@"EFENGPERSONALSHARE",@"videoName"];
            [db executeUpdate:alertStr];
            
        }

    }
}

-(void)update101:(FMDatabase* )db
{
    if(db != nil)
    {
       __block BOOL isExist = NO;
    
            [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
               
                
               FMResultSet *result = [db executeQuery:@"select count(*) as 'count' from sqlite_master where type ='table' and name = GROUPINFO"];
                NSInteger count = [result intForColumn:@"count"];
                
                if (0 == count)
                {
                    isExist = NO;
                }
                else
                {
                    isExist = YES;
                }
            }];
        
        if (isExist) {
            FMResultSet *result = [db executeQuery:@"select count(*) as 'count' from sqlite_master where type ='table' and name = GROUPINFO"];
            NSInteger count = [result intForColumn:@"count"];
            
            if (0 == count)
            {
                isExist = NO;
            }
            else
            {
                isExist = YES;
            }

        }
    }
    
    
}

-(void)doDBUpdate:(NSInteger)version db:(FMDatabase*)db
{
    char actionName[128];
    sprintf(actionName, "update%d:", version);
    SEL action = sel_registerName(actionName);
    
    [self performSelector:action withObject:db];
}

-(void)updateDBVersions:(NSString *)accountName
{
    
    if(M_ISSTRNULL(accountName))
    {
        return ;
    }
//    [db beginTransaction];
    
//    __block NSInteger dbversion = INIT_DBVERSION;  //默认初始版本
    
    //判断是否有配置系统表,如果没有配置表则建立配置表,并设置key = dbversion  value = INIT_DBVERSION

//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePath:accountName]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *adb) {
        
        
        [self update100:adb];
//        FMResultSet *resultSet = [adb executeQuery:@"SELECT param_value FROM lc_system_cfg WHERE param_name=?", @"dbversion"];
//        if(resultSet == nil)
//        {
//            BOOL ret = [adb executeUpdate:@"CREATE TABLE IF NOT EXISTS lc_system_cfg (param_name varchar(32) NOT NULL, param_value varchar(255), PRIMARY KEY (param_name) ON CONFLICT REPLACE)"];
//            ret = [adb executeUpdate:@"INSERT INTO lc_system_cfg(param_name, param_value) VALUES (?, ?)", @"dbversion", [NSString stringWithFormat:@"%d", INIT_DBVERSION]];
//        }
//        else
//        {
//            while ([resultSet next]) {
//                NSString *param_name = [resultSet stringForColumn:@"param_name"];
//                NSString *param_value = [resultSet stringForColumn:@"param_value"];
//                if([param_name isEqualToString:@"dbversion"]);
//                {
//                    dbversion = [param_value integerValue];
//                    break;
//                }
//            }
//        }
    }];
    
//    NSLog(@"dbversion:%d", dbversion);
//    
//    
//    //判断当前的版本和配置表的版本号，如果当前的大于系统配置表中的数据则根据版本号进行数据更新。
//    for (NSInteger i = dbversion + 1 ; i <= NEW_DBVERSION; i++)
//    {
//        [queen inDatabase:^(FMDatabase *db)
//         {
//            [self doDBUpdate:i db:db];
//         }];
//
//    }
////
////    //更新数据库的版本号
//    if (dbversion < NEW_DBVERSION)
//    {
//        [queen inDatabase:^(FMDatabase *db)
//         {
//             [db executeUpdate:@"UPDATE lc_system_cfg SET param_value = ? WHERE param_name = ?", [NSString stringWithFormat:@"%d", NEW_DBVERSION], @"dbversion"];
//         }];
//        
//        
//    }

    
}

@end
